#include "texto.h"

/*------------------------------------------------------------------------------
	Constructor
------------------------------------------------------------------------------*/
CTexto::CTexto()
{
	m_fEscala=1.0;
	m_aColor[0]=1.0f;
	m_aColor[1]=1.0f;
	m_aColor[2]=1.0f;	

}

/*------------------------------------------------------------------------------
	Destructor
------------------------------------------------------------------------------*/
CTexto::~CTexto()
{
}

/*------------------------------------------------------------------------------
	Initializes the object
------------------------------------------------------------------------------*/
Bool CTexto::Iniciar(char *szNombreFuente)
{
	
	if(!m_TexturaFuente.Cargar(szNombreFuente,PATH_FUENTES,GL_RGBA))
		return FALSE;
		
	m_iTamLetra=m_TexturaFuente.getAncho()/16;
	
	return TRUE;	
}

/*------------------------------------------------------------------------------
------------------------------------------------------------------------------*/
void CTexto::Liberar()
{
	m_TexturaFuente.Liberar();
	printf("- Fuente Liberada\n");
}

/*------------------------------------------------------------------------------
	Writes 2D text
------------------------------------------------------------------------------*/
void CTexto::Escribir(float fX, float fY, char string[255],ETipoAlineacion eAlin)
{
	int i=0;
	int ix,iy,iTamString;
	float colorparcial[3]={0,0,0};
	Bool fin=FALSE;
	Bool etiqueta=FALSE;
	
	
	m_TexturaFuente.Bind();	
	glLoadIdentity();
	glDisable( GL_DEPTH_TEST );		
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

	glMatrixMode( GL_PROJECTION );
	glPushMatrix();
	glLoadIdentity( );
	glOrtho( -1, 1, 1, -1, -1, 1 );
	
	glMatrixMode( GL_MODELVIEW );
    glPushMatrix();
    glLoadIdentity();
    	
	ix=string[i]%16;
	iy=string[i]/16;
	
	float px;
	glColor3f(m_aColor[0],m_aColor[1],m_aColor[2]);
	
	

	iTamString=0;
	while (!fin)		
	{	
		i++;
		
		if(i>255 || string[i]=='\n' || string[i]=='\0')
			fin=TRUE;		
		else
			iTamString++;
	}

	switch (eAlin)
	{
		case ALINEACION_DERECHA:
			glTranslatef(0,0,0);
		break;		
		case ALINEACION_CENTRO:
			glTranslatef(-(m_fEscala*(iTamString/2)),0,0);
		break;				
		case ALINEACION_IZQUIERDA:
			glTranslatef(-(m_fEscala*(iTamString)),0,0);
		break;
	}
	
//	for (i=0;i<=iTamString;i++)
	i=0;
	while(i<=iTamString)
	{			
		if(string[i]=='@' && etiqueta==TRUE)
		{
			i++;
			glColor3f(m_aColor[0],m_aColor[1],m_aColor[2]);
			colorparcial[0]=0;
			colorparcial[0]=0;
			colorparcial[0]=0;
		}
		
		if(string[i]=='@' && etiqueta==FALSE)
		{
			//printf("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX!\n");
			etiqueta=TRUE;
			i++;
			switch (string[i])
			{
				case 'r':
					colorparcial[0]=1;
				break;
				case 'g':
					colorparcial[1]=1;
				break;
				case 'b':
					colorparcial[2]=1;
				break;
			}			
			i++;
			glColor3f(colorparcial[0],colorparcial[1],colorparcial[2]);
		}
		

		
		ix=string[i]%16;
		iy=string[i]/16;
				
		px=float(fX+(i*0.0025));	
	 	DibujarLetra(fX+(i*(m_fEscala*0.9)),fY,0,ix,iy);
	 	i++;
	}
	
    glMatrixMode( GL_PROJECTION );
    glPopMatrix( );
    glMatrixMode( GL_MODELVIEW );
    glPopMatrix( );    

	glDisable(GL_ALPHA_TEST);    
    glEnable( GL_DEPTH_TEST );
    glDisable(GL_BLEND);
    glColor3f(1,1,1);
}
/*------------------------------------------------------------------------------
	Writes 3D text 
------------------------------------------------------------------------------*/
void CTexto::Escribir3D(float fX, float fY,float fZ, char string[255],ETipoAlineacion eAlin)
{
	int i=0;
	int char_actual=0;
	int ix,iy,iTamString;
	float colorparcial[3]={0,0,0};
	Bool fin=FALSE;
	Bool etiqueta=FALSE;
	
	
	m_TexturaFuente.Bind();	
	
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glDisable( GL_DEPTH_TEST );	
/*
	glMatrixMode( GL_PROJECTION );
	glPushMatrix();
	glLoadIdentity( );
	glOrtho( -1, 1, 1, -1, -1, 1 );
	
	glMatrixMode( GL_MODELVIEW );
	*/
    glPushMatrix();
    glLoadIdentity();
    	
	ix=string[i]%16;
	iy=string[i]/16;
	
	float px;
	glColor3f(m_aColor[0],m_aColor[1],m_aColor[2]);
	
	

	iTamString=0;
	while (!fin)		
	{	
		i++;
		
		if(i>255 || string[i]=='\n' || string[i]=='\0')
			fin=TRUE;		
		else
			iTamString++;
	}

	switch (eAlin)
	{
		case ALINEACION_DERECHA:
			glTranslatef(0,0,0);
		break;		
		case ALINEACION_CENTRO:
			glTranslatef(-(m_fEscala*(iTamString/2)),0,0);
		break;				
		case ALINEACION_IZQUIERDA:
			glTranslatef(-(m_fEscala*(iTamString)),0,0);
		break;
	}
	
	i=0;
	char_actual=0;
	while(i<=iTamString)
	{	
		if(string[i]=='@' && etiqueta==TRUE)
		{
			i++;
			glColor3f(m_aColor[0],m_aColor[1],m_aColor[2]);
			colorparcial[0]=0;
			colorparcial[1]=0;
			colorparcial[2]=0;
			etiqueta=FALSE;
		}
		
		if(string[i]=='@' && etiqueta==FALSE)
		{			
			etiqueta=TRUE;
			i++;
			switch (string[i])
			{
				case 'r':
					colorparcial[0]=1;
				break;
				case 'g':
					colorparcial[1]=1;
				break;
				case 'b':
					colorparcial[2]=1;
				break;
			}			
			i++;
			glColor3f(colorparcial[0],colorparcial[1],colorparcial[2]);
		}
		
		ix=string[i]%16;
		iy=string[i]/16;
		
		px=float(fX+(i*0.05));	
	 	DibujarLetra3D(fX+(char_actual*(m_fEscala*1.2)),fY,fZ,ix,iy);
	 	i++;
	 	char_actual++;
	}
	
	/*
    glMatrixMode( GL_PROJECTION );
    glPopMatrix( );
    glMatrixMode( GL_MODELVIEW );
    */
    glPopMatrix( );    

	glDisable(GL_ALPHA_TEST);        
    glDisable(GL_BLEND);
    glColor3f(1,1,1);
}


/*------------------------------------------------------------------------------
	Draws a letter
------------------------------------------------------------------------------*/
void CTexto::DibujarLetra(float fX,float fY,float fZ,int iCx,int iCy)
{
	glBegin( GL_TRIANGLES );
      	glTexCoord2f( 0.0f		+(iCx*0.0625),0.0625f	+(iCy*0.0625) );
      	glVertex3f(fX-m_fEscala, fY+m_fEscala,  fZ );
      	
      	glTexCoord2f( 0.0625f	+(iCx*0.0625),0.0625f	+(iCy*0.0625) );
      	glVertex3f(fX+m_fEscala, fY+m_fEscala,  fZ );
      	
      	glTexCoord2f( 0.0625f	+(iCx*0.0625),0.0f	+(iCy*0.0625) );
      	glVertex3f(fX+m_fEscala, fY-m_fEscala,  fZ );
      
	   	glTexCoord2f( 0.0f		+(iCx*0.0625),0.0625f	+(iCy*0.0625) );
	   	glVertex3f(fX-m_fEscala, fY+m_fEscala,  fZ );
	   	
      	glTexCoord2f( 0.0625f	+(iCx*0.0625),0.0f	+(iCy*0.0625) );
      	glVertex3f(fX+m_fEscala, fY-m_fEscala,  fZ );
      	
	   	glTexCoord2f( 0.0f		+(iCx*0.0625),0.0f	+(iCy*0.0625) );
	   	glVertex3f(fX-m_fEscala, fY-m_fEscala,  fZ );
	   	
	   	
 
    glEnd();    	
}

/*------------------------------------------------------------------------------
------------------------------------------------------------------------------*/
void CTexto::DibujarLetra3D(float fX,float fY,float fZ,int iCx,int iCy)
{
	glBegin( GL_TRIANGLES );
	   	glTexCoord2f( 0.0f		+(iCx*0.0625),0.0f	+(iCy*0.0625) );
	   	glVertex3f(fX-m_fEscala, fY+m_fEscala,  fZ );
      	glTexCoord2f( 0.0625f	+(iCx*0.0625),0.0f	+(iCy*0.0625) );
      	glVertex3f(fX+m_fEscala, fY+m_fEscala,  fZ );
      	glTexCoord2f( 0.0625f	+(iCx*0.0625),0.0625f	+(iCy*0.0625) );
      	glVertex3f(fX+m_fEscala, fY-m_fEscala,  fZ );
	   	

      	glTexCoord2f( (iCx*0.0625),(iCy*0.0625) );
      	glVertex3f(fX-m_fEscala, fY+m_fEscala,  fZ );     	     	
	   	glTexCoord2f( 0.0625f+(iCx*0.0625),0.0625f+(iCy*0.0625) );
	   	glVertex3f(fX+m_fEscala, fY-m_fEscala,  fZ );	   	
	   	glTexCoord2f((iCx*0.0625),0.0625f+(iCy*0.0625) );
      	glVertex3f(fX-m_fEscala, fY-m_fEscala,  fZ );	  
    glEnd();    	
}
